package com.equipo7.sevenmap;

import java.util.ArrayList;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

@SuppressWarnings("unchecked")
public class SevenMapItemizedOverlay extends ItemizedOverlay {

	private SevenMapView sevenMapView;
	
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	
    private Bitmap bubbleIcon, shadowIcon;
    
	private Paint	innerPaint, borderPaint, textPaint;
    
    private OverlayItem selectedItem;  
	
    public SevenMapItemizedOverlay(Drawable defaultMarker, SevenMapView sevenMapView) {
		super(boundCenterBottom(defaultMarker));
    	this.sevenMapView = sevenMapView;
		bubbleIcon = BitmapFactory.decodeResource(sevenMapView.getResources(),R.drawable.bubble);
		shadowIcon = BitmapFactory.decodeResource(sevenMapView.getResources(),R.drawable.shadow);
	}
	
	public void addOverlay(OverlayItem overlay) {
	    mOverlays.add(overlay);
	    populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	@Override
	public boolean onTap(GeoPoint p, MapView mapView)  {
		
		boolean isRemovePriorPopup = selectedItem != null;  

		selectedItem = getHitMapLocation(mapView,p);
		if ( isRemovePriorPopup || selectedItem != null) {
			mapView.invalidate();
		}		
		
		return selectedItem != null;
	}
	
    @Override
	public void draw(Canvas canvas, MapView	mapView, boolean shadow) {
   		drawItems(canvas, mapView, shadow);
   		drawInfoWindow(canvas, mapView);
    }

    private OverlayItem getHitMapLocation(MapView	mapView, GeoPoint	tapPoint) {
    	
    	OverlayItem hitMapLocation = null;
		
    	RectF hitTestRecr = new RectF();
		Point screenCoords = new Point();
    	for(OverlayItem item : mOverlays) {
    		mapView.getProjection().toPixels(item.getPoint(), screenCoords);

    		hitTestRecr.set(-bubbleIcon.getWidth()/2,-bubbleIcon.getHeight(),bubbleIcon.getWidth()/2,0);
    		hitTestRecr.offset(screenCoords.x,screenCoords.y);

    		mapView.getProjection().toPixels(tapPoint, screenCoords);
    		if (hitTestRecr.contains(screenCoords.x,screenCoords.y)) {
    			hitMapLocation = item;
    			Toast.makeText(sevenMapView.getBaseContext(), item.getSnippet(), Toast.LENGTH_SHORT).show();
    			break;
    		}
    	}
    	
    	tapPoint = null;
    	
    	return hitMapLocation; 
    }
    
    private void drawItems(Canvas canvas, MapView	mapView, boolean shadow) {
    	
		Point screenCoords = new Point();
		for(OverlayItem item : mOverlays) {	   
    		mapView.getProjection().toPixels(item.getPoint(), screenCoords);
			
	    	if (shadow) {
	    		canvas.drawBitmap(shadowIcon, screenCoords.x, screenCoords.y - shadowIcon.getHeight(),null);
	    	} else {
    			canvas.drawBitmap(bubbleIcon, screenCoords.x - bubbleIcon.getWidth()/2, screenCoords.y - bubbleIcon.getHeight(),null);
	    	}
    	}
    }

    private void drawInfoWindow(Canvas canvas, MapView	mapView) {
    	
    	if ( selectedItem != null) {
    		Point selDestinationOffset = new Point();
    		mapView.getProjection().toPixels(selectedItem.getPoint(), selDestinationOffset);

    		int INFO_WINDOW_WIDTH = 125;
    		int INFO_WINDOW_HEIGHT = 25;
    		RectF infoWindowRect = new RectF(0,0,INFO_WINDOW_WIDTH,INFO_WINDOW_HEIGHT);				
    		int infoWindowOffsetX = selDestinationOffset.x-INFO_WINDOW_WIDTH/2;
    		int infoWindowOffsetY = selDestinationOffset.y-INFO_WINDOW_HEIGHT-bubbleIcon.getHeight();
    		infoWindowRect.offset(infoWindowOffsetX,infoWindowOffsetY);

    		canvas.drawRoundRect(infoWindowRect, 5, 5, getInnerPaint());

    		canvas.drawRoundRect(infoWindowRect, 5, 5, getBorderPaint());

    		int TEXT_OFFSET_X = 10;
    		int TEXT_OFFSET_Y = 15;
    		canvas.drawText(selectedItem.getTitle(),infoWindowOffsetX+TEXT_OFFSET_X,infoWindowOffsetY+TEXT_OFFSET_Y,getTextPaint());
    	}
    }

	public Paint getInnerPaint() {
		if ( innerPaint == null) {
			innerPaint = new Paint();
			innerPaint.setARGB(225, 75, 75, 75);
			innerPaint.setAntiAlias(true);
		}
		return innerPaint;
	}

	public Paint getBorderPaint() {
		if ( borderPaint == null) {
			borderPaint = new Paint();
			borderPaint.setARGB(255, 255, 255, 255);
			borderPaint.setAntiAlias(true);
			borderPaint.setStyle(Style.STROKE);
			borderPaint.setStrokeWidth(2);
		}
		return borderPaint;
	}

	public Paint getTextPaint() {
		if ( textPaint == null) {
			textPaint = new Paint();
			textPaint.setARGB(255, 255, 255, 255);
			textPaint.setAntiAlias(true);
		}
		return textPaint;
	}
	
}
